﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Empresas_Cadastros__wf_wfFolhaPagamento : System.Web.UI.Page
{
    protected int idWfConfig = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["IdWfConfig"] != null)
            idWfConfig = Int32.Parse(Request.QueryString["IdWfConfig"].ToString());

        if (!IsPostBack)
        {
            ExibeParametros();
        }

        AtivaAjax();
    }

    #region Eventos

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        CarregarTudo();
    }

    #region Alerta
    protected void GridAlerta_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
        wfParametro.Excluir();

        CarregarTudo();
    }
    protected void GridAlerta_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridCommandItem)
        {
            int total = 0;

            if (e.Item.OwnerTableView.DataSourceCount != null)
                total = e.Item.OwnerTableView.DataSourceCount;

            if (total >= 4)
            {
                HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                btnInsert.Visible = false;
            }
        }
    }
    #endregion

    #region Pendencia
    protected void GridPendencia_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
        wfParametro.Excluir();

        CarregarTudo();
    }
    protected void GridPendencia_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridCommandItem)
        {
            int total = 0;

            if (e.Item.OwnerTableView.DataSourceCount != null)
                total = e.Item.OwnerTableView.DataSourceCount;

            if (total >= 4)
            {
                HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                btnInsert.Visible = false;
            }
        }
    }
    #endregion

    #endregion

    #region Metodos
    private void AtivaAjax()
    {
        WorkflowConfigData wfConfigData = new WorkflowConfigData();
        List<WorkflowConfigData> listWkConfigData = wfConfigData.SelectPor(idWfConfig);

        foreach (WorkflowConfigData item in listWkConfigData)
        {
            switch (item.TipoPagamento.IdTipoPagamento)
            {
                case 1:
                    //Pagamento Principal
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTpPagPrinA, RadAjaxLoadingPanel1);
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTpPagPrinP, RadAjaxLoadingPanel1);

                    break;
                case 2:
                    //Adiantamento
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTpAdiantA, RadAjaxLoadingPanel1);
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTpAdiantP, RadAjaxLoadingPanel1);

                    break;
                case 3:
                    //Pagamento Unico
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridtpPagUnicoA, RadAjaxLoadingPanel1);
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridtpPagUnicoP, RadAjaxLoadingPanel1);
                    break;
                case 4:
                    //13° - Pagamento 1° Parcela
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTp113A, RadAjaxLoadingPanel1);
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTp113P, RadAjaxLoadingPanel1);
                    break;
                case 5:
                    //13° - Pagamento 2° Parcela
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTp213A, RadAjaxLoadingPanel1);
                    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridTp213P, RadAjaxLoadingPanel1);
                    break;
                default:
                    break;
            }
        }
    }
    private void ExibeParametros()
    {
        //1	Pagamento Principal
        //2	Pagamento Adiantamento
        //3	Pagamento Unico
        //4	13° - Pagamento 1° Parcela
        //5	13° - Pagamento 2ª Parcela

        dvPagUnico.Visible = false;
        dvPagPrincipal.Visible = false;
        dvAdiantamento.Visible = false;
        dv113.Visible = false;
        dv213.Visible = false;

        WorkflowConfigData wfConfigData = new WorkflowConfigData();
        List<WorkflowConfigData> listWkConfigData = wfConfigData.SelectPor(idWfConfig);

        foreach (WorkflowConfigData item in listWkConfigData)
        {
            switch (item.TipoPagamento.IdTipoPagamento)
            {
                case 1:
                    dvPagPrincipal.Visible = true;
                    CarregarPagPrincipalAlerta(item.IdWfConfigData);
                    CarregarPagPrincipalAviso(item.IdWfConfigData);
                    hIdWfConfigData_tpPagPrin.Value = item.IdWfConfigData.ToString();
                    break;
                case 2:
                    dvAdiantamento.Visible = true;
                    CarregarAdiantamentoAlerta(item.IdWfConfigData);
                    CarregarAdiantamentoAviso(item.IdWfConfigData);
                    hIdWfConfigData_tpAdiant.Value = item.IdWfConfigData.ToString();
                    break;
                case 3:
                    dvPagUnico.Visible = true;
                    CarregarPagUnicoAlerta(item.IdWfConfigData);
                    CarregarPagUnicoAviso(item.IdWfConfigData);
                    hdIdWfConfigData_tpPagUnico.Value = item.IdWfConfigData.ToString();
                    break;
                case 4:
                    dv113.Visible = true;
                    Carregar113Alerta(item.IdWfConfigData);
                    Carregar113Aviso(item.IdWfConfigData);
                    hIdWfConfigData_tp113.Value = item.IdWfConfigData.ToString();
                    break;
                case 5:
                    dv213.Visible = true;
                    Carregar213Alerta(item.IdWfConfigData);
                    Carregar213Aviso(item.IdWfConfigData);
                    hIdWfConfigData_tp213.Value = item.IdWfConfigData.ToString();
                    break;
                default:
                    break;
            }
        }
    }
    private void CarregarTudo()
    {
        //Pagamenoto Unico
        if (Convert.ToInt32(hdIdWfConfigData_tpPagUnico.Value) != 0)
        {
            CarregarPagUnicoAlerta(Convert.ToInt32(hdIdWfConfigData_tpPagUnico.Value));
            CarregarPagUnicoAviso(Convert.ToInt32(hdIdWfConfigData_tpPagUnico.Value));
        }

        //Adiantamento
        if (Convert.ToInt32(hIdWfConfigData_tpPagPrin.Value) != 0)
        {
            CarregarPagPrincipalAlerta(Convert.ToInt32(hIdWfConfigData_tpPagPrin.Value));
            CarregarPagPrincipalAviso(Convert.ToInt32(hIdWfConfigData_tpPagPrin.Value));
        }

        //Adiantamento
        if (Convert.ToInt32(hIdWfConfigData_tpAdiant.Value) != 0)
        {
            CarregarAdiantamentoAlerta(Convert.ToInt32(hIdWfConfigData_tpAdiant.Value));
            CarregarAdiantamentoAviso(Convert.ToInt32(hIdWfConfigData_tpAdiant.Value));
        }

        //1º Parcela - 13º Salário
        if (Convert.ToInt32(hIdWfConfigData_tp113.Value) != 0)
        {
            Carregar113Alerta(Convert.ToInt32(hIdWfConfigData_tp113.Value));
            Carregar113Aviso(Convert.ToInt32(hIdWfConfigData_tp113.Value));
        }

        //2º Parcela - 13º Salário
        if (Convert.ToInt32(hIdWfConfigData_tp213.Value) != 0)
        {
            Carregar213Alerta(Convert.ToInt32(hIdWfConfigData_tp213.Value));
            Carregar213Aviso(Convert.ToInt32(hIdWfConfigData_tp213.Value));
        }
    }
    private List<WorkflowConfigParametro> RetornaWfParametroAlerta(int id, string tipo)
    {
        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listParametrosAlerta = wfParametro.SelectAlerta(id, tipo);

        return listParametrosAlerta;
    }
    private List<WorkflowConfigParametro> RetornaWfParametroAviso(int id, string tipo)
    {
        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listParametrosAlerta = wfParametro.SelectAviso(id, tipo);

        return listParametrosAlerta;
    }

    #region Pagamento Unico
    private void CarregarPagUnicoAlerta(int idWfConfigData)
    {
        GridtpPagUnicoA.DataSource = RetornaWfParametroAlerta(idWfConfigData, "Composto");
        GridtpPagUnicoA.DataBind();
    }
    private void CarregarPagUnicoAviso(int idWfConfigData)
    {
        GridtpPagUnicoP.DataSource = RetornaWfParametroAviso(idWfConfigData, "Composto");
        GridtpPagUnicoP.DataBind();
    }
    #endregion

    #region Adiantamento
    private void CarregarAdiantamentoAlerta(int idWfConfigData)
    {
        GridTpAdiantA.DataSource = RetornaWfParametroAlerta(idWfConfigData, "Composto");
        GridTpAdiantA.DataBind();
    }
    private void CarregarAdiantamentoAviso(int idWfConfigData)
    {
        GridTpAdiantP.DataSource = RetornaWfParametroAviso(idWfConfigData, "Composto");
        GridTpAdiantP.DataBind();
    }
    #endregion

    #region Pagamento Principal
    private void CarregarPagPrincipalAlerta(int idWfConfigData)
    {
        GridTpPagPrinA.DataSource = RetornaWfParametroAlerta(idWfConfigData, "Composto");
        GridTpPagPrinA.DataBind();
    }
    private void CarregarPagPrincipalAviso(int idWfConfigData)
    {
        GridTpPagPrinP.DataSource = RetornaWfParametroAviso(idWfConfigData, "Composto");
        GridTpPagPrinP.DataBind();
    }
    #endregion

    #region 1º Parcela - 13º Salário
    private void Carregar113Alerta(int idWfConfigData)
    {
        GridTp113A.DataSource = RetornaWfParametroAlerta(idWfConfigData, "Composto");
        GridTp113A.DataBind();
    }
    private void Carregar113Aviso(int idWfConfigData)
    {
        GridTp113P.DataSource = RetornaWfParametroAviso(idWfConfigData, "Composto");
        GridTp113P.DataBind();
    }
    #endregion

    #region 2º Parcela - 13º Salário
    private void Carregar213Alerta(int idWfConfigData)
    {
        GridTp213A.DataSource = RetornaWfParametroAlerta(idWfConfigData, "Composto");
        GridTp213A.DataBind();
    }
    private void Carregar213Aviso(int idWfConfigData)
    {
        GridTp213P.DataSource = RetornaWfParametroAviso(idWfConfigData, "Composto");
        GridTp213P.DataBind();
    }
    #endregion

    #endregion
}
